package com.fancon.android.gardenjokesx.adapter;

import java.net.URLDecoder;
import java.util.ArrayList;

import net.arnx.jsonic.JSON;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.fancon.android.emoji.widget.RichTextView;
import com.fancon.android.gardenjokesx.R;
import com.fancon.android.gardenjokesx.activity.DetailJokesActivity;
import com.fancon.android.gardenjokesx.entity.Photo;
import com.fancon.android.gardenjokesx.utils.Constant;
import com.fancon.android.gardenjokesx.utils.StringUtil;
import com.fancon.android.gardenjokesx.utils.TimeUtil;
import com.fancon.android.gardenjokesx.widget.IGlobalStorage;
import com.fancon.android.multithread.RequestUI;
import com.fancon.android.ui.widget.ImageCacheView;

public class JokesAdapter extends BaseAdapter implements Filterable {
	private LayoutInflater mInflater;
	private Context context;
	private ArrayList<Photo> lisPhoto;

	public JokesAdapter(Context context, ArrayList<Photo> lisPhoto) {
		mInflater = LayoutInflater.from(context);
		this.context = context;
		this.lisPhoto = lisPhoto;
	}

	public ArrayList<Photo> getLisPhoto() {
		return lisPhoto;
	}

	public void setLisPhoto(ArrayList<Photo> lisPhoto) {
		this.lisPhoto = lisPhoto;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		int ret = lisPhoto.size();
		return ret;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int arg0, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		try {
			final Photo photo = lisPhoto.get(arg0);
			final ViewHolder holder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.joke_item, null);
				holder = new ViewHolder();
				holder.thumb = (ImageCacheView) convertView
						.findViewById(R.id.cm_listphotos_imageview);
				holder.caption = (TextView) convertView
						.findViewById(R.id.comment);
				holder.userTimePost = (TextView) convertView
						.findViewById(R.id.user_time_post);
				convertView.setTag(holder);
				holder.likeComment = (TextView) convertView
						.findViewById(R.id.like_comment);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (!photo.getUrl_photo().equals(Constant.NO_COMIC)) {
				/*
				 * ImageLoadingListener loading = new ImageLoadingListener() {
				 * 
				 * @Override public void onLoadingStarted() { // TODO
				 * Auto-generated method stub }
				 * 
				 * @Override public void onLoadingFailed() { // TODO
				 * Auto-generated method stub
				 * holder.thumb.setVisibility(View.GONE); }
				 * 
				 * @Override public void onLoadingComplete() { // TODO
				 * Auto-generated method stub } };
				 */
				// holder.thumb.setImageLoadingListener(loading);
				holder.thumb.setLoadingImg(R.drawable.loading);
				holder.thumb.getmImage().setScaleType(ScaleType.CENTER_CROP);
				holder.thumb.loadImage(StringUtil.processImageLink(photo
						.getUrl_photo()));
				holder.thumb.setVisibility(View.VISIBLE);
			} else {
				holder.thumb.setVisibility(View.GONE);
			}

			String userTimeStr = context.getString(R.string.user_time_post);
			String likeCommentStr = context.getString(R.string.like_commentx);
			holder.userTimePost.setText(String.format(userTimeStr,
					StringUtil.getDisplayName(photo.getUser()),
					TimeUtil.getTimeCount(photo.getTime_post(), context)));
			holder.likeComment.setText(String.format(likeCommentStr,
					StringUtil.getPostCount(photo.getNumber_click()),
					StringUtil.getPostCount(photo.getNumber_comment())));
			// holder.caption.setText(StringUtil.getDisplayContent(URLDecoder.decode(photo.getCaption(),"UTF-8"),
			// context));
			//Reset comment
			holder.caption.setText("");
//			LoadEmojiThread req = new LoadEmojiThread("like_req",
//					(Activity) context, photo.getCaption(), holder.caption);
//			((IFanconGlobalState) context).getRequestQueue().addRequest(req);
			 holder.caption.setText(StringUtil.getDisplayContent(URLDecoder.decode(photo.getCaption(),"UTF-8"),
			 context));
			final String jsonPhoto = JSON.encode(photo);
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(context,
							DetailJokesActivity.class);
					intent.putExtra("photo", jsonPhoto);
					context.startActivity(intent);
					//save current position
					((IGlobalStorage)(((Activity)(context)).getApplication())).setCurrentPosition(arg0);
				}
			});

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return convertView;
	}

	@Override
	public Filter getFilter() {
		// TODO Auto-generated method stub
		return null;
	}

	static class ViewHolder {
		ImageCacheView thumb;
		TextView caption;
		TextView userTimePost;
		TextView likeComment;
	}

	class LoadEmojiThread extends RequestUI {
		private Activity context;
		private String utf8;
		private RichTextView txt;

		public LoadEmojiThread(Object key, Activity activity, String utf8,
				RichTextView txt) {
			super(key, activity);
			this.context = activity;
			this.utf8 = utf8;
			this.txt = txt;
		}

		@Override
		public void execute() throws Exception {
			try {
				utf8 = StringUtil.getDisplayContent(
						URLDecoder.decode(utf8, "UTF-8"), context);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}

		@Override
		public void executeUI(Exception ex) {
			// TODO Auto-generated method stub
			try {
				txt.setText(utf8);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		}

	}
}
